package cn.oversky.eom;

import cn.oversky.eom.utils.PropertiesUtils;
import com.alibaba.fastjson.JSONObject;
import com.sun.istack.internal.logging.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @email CorvusY@foxmail.com
 * @date 2017/5/27 15:22.
 * Now is history !
 */
public class ThDotConvert implements IExcelRowConfig {

    private Logger logger = Logger.getLogger( ThDotConvert.class );

    /**
     * 集合泛型与属性的映射
     * 用途：如果是集合类型，则将集合类型的泛型放进来。
     */
    private Map<String, Class> collectionGenericTypes = new HashMap<String, Class>();

    private Map<String, IExcelObjInspector> collectionObjInspector = new HashMap<String, IExcelObjInspector>();

    public ThDotConvert() {
    }

    public ThDotConvert( String json ) {
        Map<String, String> jsonObject = JSONObject.parseObject(json, Map.class );
        for( Map.Entry<String, String> me : jsonObject.entrySet() ) {
            String key = me.getKey();
            String className = me.getValue();
            try {
                putCollectionGenericType( key, Class.forName( className ) );
            } catch (ClassNotFoundException e) {
                logger.warning( "请检查子属性的类是否存在", e );
            }
        }
    }

    /**
     * 多表头拼接分隔符
     */
    private static final String SEP = ".";

    /**
     * 表头的行，从 0 开始计数
     */
    private long thRows;

    @Override
    public long getThRows() {
        return thRows;
    }

    @Override
    public void setThRows(long thRows) {
        this.thRows = thRows;
    }

    @Override
    public long convert( List<Row> rows ) {
        Row cells = rows.get((int) thRows );
        cellsDotCore( rows, cells );
        return thRows;
    }

    @Override
    public List<Row> reconvert(List<Row> wb) {
        return null;
    }

    @Override
    public Class getCollectionGenericType(String fieldName) {
        return this.collectionGenericTypes.get( fieldName );
    }

    @Override
    public void putCollectionGenericType(String fieldName, Class genericType) {
        this.collectionGenericTypes.put( fieldName, genericType );
    }

    @Override
    public void putCollectionObjInspector(String fieldName, IExcelObjInspector objInspector) {
        this.collectionObjInspector.put( fieldName, objInspector );
    }

    @Override
    public void putCGTAndCOI(String fieldName, Class genericType, IExcelObjInspector objInspector) {
        this.putCollectionGenericType( fieldName, genericType );
        this.putCollectionObjInspector( fieldName, objInspector );
    }

    @Override
    public IExcelObjInspector getCollectionObjInspector(String fieldName) {
        return collectionObjInspector.get( fieldName );
    }

    public void cellsDotCore( List<Row> rows, Row cells ) {
        int rowNum = cells.getRowNum();
        if( rowNum == 0 ) return;
        // 对处理行逐个获取单元格，并设值
        Row preCells = rows.get(rowNum - 1);
        // 先将前行处理拼接
        cellsDotCore(rows, preCells);
        // 开始行处理
        for( Cell cell : cells ) {
            String curValue = cell.getStringCellValue();
            int idx = cell.getColumnIndex();
            String preValue = findPreRowAndPreCellValue( preCells, idx );
            String sep = PropertiesUtils.isNotNull(curValue) ? SEP : "";
            cell.setCellValue( preValue + sep + curValue );
        }
    }

    public String findPreRowAndPreCellValue( Row preCells, int idx ) {
        String cellValue = "";
        // 获取前推，最近的一个有值单元格的值
        for( Cell cell : preCells ) {
            int columnIndex = cell.getColumnIndex();
            if( columnIndex <= idx ) {
                String stringCellValue = cell.getStringCellValue();
                if( null != stringCellValue && !"".equals( stringCellValue ) ) {
                    cellValue = stringCellValue;
                }
            }
        }
        return cellValue;
    }
}
